import { prizes } from '../constants'

export const getDate = () => {
  const today = new Date();
  const year = today.getFullYear();
  const month = String(today.getMonth() + 1).padStart(2, '0');
  const day = String(today.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`;
}

export const getDateTime = () => {
  const now = new Date();
  const hours = now.getHours();        // 小时 (0-23)
  const minutes = now.getMinutes();    // 分钟 (0-59)
  const seconds = now.getSeconds();    // 秒 (0-59)
  const time = `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
  return `${getDate()} ${time}`
}

export const lottery = function (weightSum, prizeWeight) {
  let res = prizes[prizes.length - 1];
  const random = Math.random() * weightSum;
  const concatWeightArr = prizeWeight.concat(random);
  const sortedWeightArr = concatWeightArr.sort(function (a, b) { return a - b; });
  let randomIndex = sortedWeightArr.indexOf(random);
  randomIndex = Math.min(randomIndex, prizes.length - 1);
  res = prizes[randomIndex];
  return { "weightSum": weightSum, "weightRandom": random, prizeIndex: randomIndex, "data": res };
};

export const sum = (arr) => {
  return arr.reduce(function (prev, currVal) {
    return prev + currVal;
  }, 0);
}